package string_question.leecode.test3;

import java.util.HashSet;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/6 16:48
 */
public class Solution1 {

    // 双指针 + HashSet
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) return 0;
        HashSet<Character> set = new HashSet<>();
        int l = 0, r = 0, res = 1;
        char c = ' ';
        while (r < s.length()) {
            c = s.charAt(r);
            if (set.contains(c)) {
                res = Math.max(r - l, res);
                set.remove(s.charAt(l++));
            } else {
                set.add(c);
                r++;
            }
        }
        res = Math.max(res, r - l);   // 注意：这一步忘了加了
        return res;
    }

}
